package cn.bonoon.kernel.web.models;

import javax.servlet.http.HttpServletRequest;

import cn.bonoon.kernel.util.MD5Util;
import cn.bonoon.kernel.util.StringHelper;

/**
 * 定义弹出对话框
 * @author jackson
 *
 */
public final class DialogModel extends AbstractModel {
	
	private final static String DEFAULT_DIALOG_NAME = "di";
	private final static String DEFAULT_DIALOG_MASTER = "layout-dialog.vm";
	private final static String DEFAULT_FORM_NAME = "form";
	private final static String DEFAULT_ID = MD5Util.randomMD5String();
	
	private final String id;
	private final StringBuilder dlgattrs = new StringBuilder();
	
	private String formName = DEFAULT_FORM_NAME;
	
	//---------下面三个属性应该被移走
	private int width = 0;
	private int height = 0, maxHeight;
	private boolean modal = true;
	
	/**
	 * 当对话框关闭的时候需要执行的事件
	 */
	private StringBuilder onClosedEvent;
	/**
	 * 在对话框加载完成的时候执行的事件
	 */
	private StringBuilder onLoadEvent;

	public void setTitle(String title) {
		if(StringHelper.isNotEmpty(title)){
			dlgattrs.append(" title='").append(title).append('\'');
		}
	}
	
	public DialogModel(HttpServletRequest request) {
		this(DEFAULT_ID, DEFAULT_ID, request);
	}
	
	public DialogModel(String mid, HttpServletRequest request) {
		this(mid, mid, request);
	}
	
	public DialogModel(String id, String mid, HttpServletRequest request) {
		super(mid, request);
		this.id = id;
		setMaster(DEFAULT_DIALOG_MASTER);
		setModelName(DEFAULT_DIALOG_NAME);
	}

	public String getId() {
		return id;
	}

	public void addForm(Object obj) {
		addObject(formName, obj);
	}

	public void setIconCls(String iconCls) {
		dlgattrs.append(" iconCls='").append(iconCls).append('\'');
	}
	
	public void addDialogAttrs(StringBuilder attrs){
		if(null != attrs){
			dlgattrs.append(attrs);
		}
	}
	
	public void addHtml(StringBuilder html){
		this.includes = merger(this.includes, html);
	}
	
	@Override
	public DialogModel execute(String view) {
		dlgattrs.append(" style='");
		if(width > 0) dlgattrs.append("width:").append(width).append("px;");
		if(height > 0) dlgattrs.append("height:").append(height).append("px;");
		if(maxHeight > 0) dlgattrs.append("max-height:").append(maxHeight).append("px;");
		dlgattrs.append("' modal='").append(Boolean.toString(modal)).append('\'');
		super.execute(view);
		return this;
	}
	
	public StringBuilder getDialogAttributes() {
		return dlgattrs;
	}
	
	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}
	
	public void setHeight(int height){
		this.height = Math.max(this.height, height);
	}
	
	public void setWidth(int width){
		this.width = Math.max(this.width, width);
	}

	public void setSize(int width, int height) {
		setHeight(height);
		setWidth(width);
	}

	public void setSize(Size size) {
		setHeight(size.getHeight());
		setWidth(size.getWidth());
	}

	public void setSize(int size) {
		setSize(size, size);
	}
	/**
	 * <pre>
	 * 对话框是否可关闭，显示右上角的关闭按钮
	 * 
	 * 注意与{@link setCloseButton}的区别
	 * 两者可以分别设置
	 * 
	 * 只允许被调用一次，否则会添加多个属性
	 * </pre>
	 * @param closable
	 */
	public void setClosable(boolean closable) {
		if(closable){
			dlgattrs.append(" closable='true'");
		}else{
			dlgattrs.append(" closable='false'");
		}
	}
	
	/**
	 * 设置是否模式对话框
	 * @param modal
	 */
	public void setModal(boolean modal) {
		this.modal = modal;
	}

	public StringBuilder getOnClosedEvent() {
		return onClosedEvent;
	}

	public void addClosedEvent(StringBuilder onClosedEvent) {
		this.onClosedEvent = merger(this.onClosedEvent, onClosedEvent);
	}

	public void addClosedEvent(String onClosedEvent) {
		if(null != onClosedEvent){
			if(null == this.onClosedEvent){
				this.onClosedEvent = new StringBuilder();
			}
			this.onClosedEvent.append(onClosedEvent);
		}
	}

	public StringBuilder getOnLoadEvent() {
		return onLoadEvent;
	}

	public void addLoadEvent(StringBuilder onLoadEvent) {
		this.onLoadEvent = merger(this.onLoadEvent, onLoadEvent);
	}
}
